package com.ft.turorial.spring.boot.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @Configuration、@Controller、@Service 等被Spring管理的类都支持，注意重写的方法 setEnvironment
 *
 *                                     是在系统启动的时候被执行
 * 
 * @author lc
 *
 */
@Configuration
public class MyWebAppConfigurer implements EnvironmentAware {

	private static final Logger logger = LoggerFactory.getLogger(MyWebAppConfigurer.class);

	private RelaxedPropertyResolver propertyResolver;

	@Value("${spring.datasource.url}")
	private String myUrl;

	/**
	 * 这个方法只是测试实现EnvironmentAware接口，读取环境变量的方法。
	 */
	@Override
	public void setEnvironment(Environment env) {
		logger.info(env.getProperty("JAVA_HOME"));
		logger.info(myUrl);
		String str = env.getProperty("spring.datasource.url");
		logger.info(str);
		String root = env.getProperty("spring.datasource.username");
		logger.info(root);
		propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");
		String url = propertyResolver.getProperty("url");
		logger.info(url);
		String password = propertyResolver.getProperty("password");
		logger.info(password);
	}
}
